package com.tengju.bff.interfaces.manage.crm.chief;

import com.tengju.bff.interfaces.manage.crm.chief.facade.ChiefClubFacade;
import com.tengju.bff.interfaces.manage.crm.chief.form.AddChiefClubRelationForm;
import com.tengju.bff.interfaces.manage.crm.chief.form.RemoveChiefClubRelationForm;
import com.tengju.bff.interfaces.manage.crm.chief.vo.ChiefClubVO;
import com.tengju.bff.interfaces.manage.crm.chief.vo.GetChiefClubRelationForm;
import com.tengju.bff.interfaces.manage.crm.chief.vo.UpdateChiefClubRelationDTO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.StringConvertUtil;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.support.domain.annotation.BizLog;
import com.tengju.support.domain.annotation.BizLogSysName;
import com.tengju.user.application.chief.ChiefClubService;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.chief.ChiefClubRelationId;
import com.tengju.user.domain.model.chief.ChiefId;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@Api(tags = "总监俱乐部关系管理")
@RequestMapping(value = "manage/crm/chief")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ChiefClubController {

    private final ChiefClubFacade chiefClubFacade;
    private final ChiefClubService chiefClubService;

    @ApiOperation(value = "总监俱乐部查询", notes = "总监俱乐部查询")
    @RequestMapping(value = "club", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<ChiefClubVO> getChiefClub(Long chiefId) {
        try {
            return ApiResponse.newSuccess(chiefClubFacade.getChiefClub(new ChiefId(chiefId)));
        } catch (DomainException | ApplicationException e) {
            log.error("总监俱乐部查询失败,chiefId:{}", chiefId, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "总监添加管理俱乐部", notes = "总监添加管理俱乐部")
    @RequestMapping(value = "addClub", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addClub(@RequestParam("chiefId") Long chiefId,
                                        @RequestParam("clubIds") List<Long> clubIds) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(chiefClubService.addClub(staff, new ChiefId(chiefId), clubIds));
        } catch (DomainException | ApplicationException e) {
            log.error("总监添加管理俱乐部失败,chiefId:{}", chiefId, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "总监删除管理俱乐部", notes = "总监删除管理俱乐部")
    @RequestMapping(value = "removeClub", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> removeClub(@RequestParam("chiefId") Long chiefId,
                                           @RequestParam("clubIds") List<Long> clubIds) {
        try {
            return ApiResponse.newSuccess(chiefClubService.removeClub(new ChiefId(chiefId), clubIds));
        } catch (DomainException | ApplicationException e) {
            log.error("总监删除管理俱乐部失败,chiefId:{}", chiefId, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "添加总监俱乐部关系", notes = "添加总监俱乐部关系")
    @RequestMapping(value = "addChiefClubRelation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addChiefClubRelation(@Validated @RequestBody AddChiefClubRelationForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(chiefClubService.addChiefClubRelation(staff, new ChiefId(form.getChiefId()),
                    StringConvertUtil.stringConvertToLongList(form.getClubIds())));
        } catch (DomainException | ApplicationException e) {
            log.error("总监添加管理俱乐部失败,AddChiefClubRelationForm:{}", form, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @NotTokenValid
    @ApiOperation(value = "查询总监俱乐部关系", notes = "查询总监俱乐部关系")
    @RequestMapping(value = "queryChiefClubRelationByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> queryChiefClubRelationByPage(GetChiefClubRelationForm getChiefClubRelationForm) {
        try {
            return ApiResponse.newSuccess(chiefClubFacade.queryChiefClubRelationByPage(getChiefClubRelationForm));
        } catch (DomainException | ApplicationException e) {
            log.error("总监俱乐部关系查询失败,chiefClubRelationDTO:{}", getChiefClubRelationForm, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "删除总监俱乐部关系", notes = "删除总监俱乐部关系")
    @RequestMapping(value = "removeChiefClubRelation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> removeChiefClubRelation(@Validated @RequestBody RemoveChiefClubRelationForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            ChiefClubRelationId chiefClubRelationId = new ChiefClubRelationId(form.getChiefClubRelationId());
            chiefClubService.removeChiefClubRelation(chiefClubRelationId);
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("删除总监俱乐部关系失败,RemoveChiefClubRelationForm:{}", form, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "修改总监俱乐部关系", notes = "修改总监俱乐部关系")
    @RequestMapping(value = "updateChiefClubRelation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> updateChiefClubRelationByPage(@Validated @RequestBody UpdateChiefClubRelationDTO updateChiefClubRelationDTO) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(chiefClubFacade.updateChiefClubRelation(staff, updateChiefClubRelationDTO));
        } catch (DomainException | ApplicationException e) {
            log.error("总监俱乐部关系修改失败,updateChiefClubRelationDTO:{}", updateChiefClubRelationDTO, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

}
